package com.huarui.task.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.huarui.task.dao.TaskDao;
import com.huarui.task.dao.TaskDetailDao;
import com.huarui.task.dao.TaskOwnerDao;
import com.huarui.task.model.TaskDetail;
import com.huarui.task.service.TaskDetailService;

public class TaskDetailServiceImpl implements TaskDetailService {

	private static final Logger log = org.apache.log4j.Logger.getLogger(TaskDetailServiceImpl.class);
	private TaskDao taskDao;

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public TaskDetailDao taskDetailDao;
	private TaskOwnerDao taskOwnerDao;

	public TaskOwnerDao getTaskOwnerDao() {
		return taskOwnerDao;
	}

	public void setTaskOwnerDao(TaskOwnerDao taskOwnerDao) {
		this.taskOwnerDao = taskOwnerDao;
	}

	private TransactionTemplate transactionTemplate;

	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	public TaskDetailDao getTaskDetailDao() {
		return taskDetailDao;
	}

	public void setTaskDetailDao(TaskDetailDao taskDetailDao) {
		this.taskDetailDao = taskDetailDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.task.service.impl.TaskDetailService#rollback(java.lang.Integer
	 * , java.lang.String)
	 */
	public TaskDetail rollback(final Integer currentTaskDetailId, final String status) {

		try {
			TaskDetail result = (TaskDetail) transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus s) {

					TaskDetail currentDetail = (TaskDetail) taskDetailDao.queryById(currentTaskDetailId, true);
					if (currentDetail == null) {
						throw new IllegalArgumentException("No sucha a taskdetail id:" + currentTaskDetailId);
					}

					TaskDetail taskDetail = taskDetailDao.findRollbackTaskDetail(currentDetail.getTaskId(), status);

					if (taskDetail == null) {
						s.setRollbackOnly();
						return null;

					}

					// taskDetail.
					taskDetail.setPreviouseId(currentTaskDetailId);
					taskDetail.setInitStatusName(currentDetail.getEndStatusName());
					taskDetail.setEndStatusName(status);

					taskDetailDao.create(taskDetail);

					return taskDetail;
				}
			});

			return result;
		} catch (RuntimeException e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.task.service.impl.TaskDetailService#appendTransmitTaskDetail
	 * (java.lang.Integer, java.lang.String)
	 */
	public void appendTransmitTaskDetail(final Integer taskId, final String statusName) {
		transactionTemplate.execute(new TransactionCallback() {
			@Override
			public Object doInTransaction(TransactionStatus s) {

				TaskDetail taskDetail = taskDetailDao.findNewTaskDetail(taskId);

				taskDetail.setPreviouseId(taskDetail.getId());
				taskDetail.setInitStatusName(taskDetail.getEndStatusName());
				taskDetail.setEndStatusName(statusName);

				taskDetailDao.create(taskDetail);

				return null;
			}
		});
	}

	public List<TaskDetail> getTaskDetails(final Integer taskId) {
		return this.taskDetailDao.findTaskDetails(taskId);
	}

}
